package com.ndhutu.ee.util;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 */
public class StringUtil {
    private static String[] letter = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "X", "Y", "Z"};

    /**
     * 获取一个随机数字
     * @return
     */
    public static int getRandomNumber(){
        return (int)(Math.random() * 10);
    }

    /**
     * 获取随机数字字符串
     * @param num
     * @return
     */
    public static String getRandomNumberStr(int num){
        StringBuffer buff = new StringBuffer();
        while (num-- > 0){
            buff.append(getRandomNumber());
        }
        return buff.toString();
    }

    /**
     * 获取6位数字验证码
     * @return
     */
    public static String getRandomNumberStr(){
        return getRandomNumberStr(6);
    }

    /**
     * 判空
     * @param o
     * @return
     */
    public static boolean isEmpty(Object o){
        if(o == null || "".equals(o)){
            return true;
        }
        return false;
    }

    /**
     * 获取32位唯一字符串
     * @Author Shane
     * @Date 2019/7/9 23:55
     * @param
     * @return
     */
    public static String getUUIDStr(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取邀请码，由两位字符四位数字组成
     * @Author Shane
     * @Date 2019/7/1110:23
     * @return
     **/
    public static String getInviteCode(){
        StringBuffer buff = new StringBuffer();
        for(int i=2; i>0; i--){
            buff.append(letter[(int)(Math.random()*letter.length)]);
        }
        buff.append(getRandomNumberStr(4));
        return buff.toString();
    }

    /**
     * 生成订单号(16位)
     * @Author Shane
     * @Date 2019/8/2214:46
     * @param
     * @return
     **/
    public static String getOrderNumber(){
        return System.currentTimeMillis()+getRandomNumberStr(3);
    }

    /**
     * 随机获取文件上传路径
     * @Author Shane
     * @Date 2019/9/15 17:30
     * @param
     * @return
     */
    public static String getRandomUploadPath(){
        Date date = new Date();

        StringBuffer buffer = new StringBuffer();
        buffer.append(DateUtil.format(date,"yyyy")).append(File.separator).append(DateUtil.format(date, "MM")).append(File.separator)
                .append(DateUtil.format(date, "dd")).append(File.separator).append(DateUtil.format(date, "HHmmss")).append(File.separator);
        return buffer.toString();
    }

    /**
     * 版本比较
     * @Author Shane
     * @Date 2019/11/29 16:06
     * @param version1  版本1
     * @param version2  版本2
     * @return  大于返回1，小于返回-1，等于返回0
     **/
    public static int compareVersion(String version1, String version2) {
        List<String> list1 = new ArrayList(Arrays.asList(version1.split("\\.")));
        List<String> list2 = new ArrayList(Arrays.asList(version2.split("\\.")));
        int size1 = list1.size(), size2 = list2.size();
        if(size1 > size2){
            for(int i=0,size=size1-size2; i<size; i++){
                list2.add("0");
            }
        }else if(size1 < size2){
            for(int i=0,size=size2-size1; i<size; i++){
                list1.add("0");
            }
        }
        for(int i=0, size=list1.size(); i<size; i++){
            int curr = Integer.parseInt(list1.get(i)), sys = Integer.parseInt(list2.get(i));
            if(curr > sys){
                return 1;
            }else if(curr < sys){
                return -1;
            }
        }
        return 0;
    }

    /**
     * 判断字符串中是否包含指定正则内容
     * @Author Shane
     * @Date 2020/7/12 13:32
     * @param str   字符串
     * @param regex 正则表达式
     * @return
     */
    public static boolean matcher(String str, String regex){
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 判断字符串中是否存在链接
     * @Author AppiJ
     * @Date 2020/12/28 17:14
     * @param
     * @return
     */
    public static boolean checkLink(String str){
        boolean result = false;
        //把中文替换为#
        str = str.replaceAll("[\u4E00-\u9FA5]", "#");
        String url[]=str.split("#");
        //转换为小写
        if(url!=null&&url.length>0){
            for(String tempurl:url){
                if(StringUtils.isBlank(tempurl)){
                    continue;
                }
                tempurl = tempurl.toLowerCase();
                String regex = "^((https|http|ftp|rtsp|mms)?://)"
                        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
                        + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
                        + "|" // 允许IP和DOMAIN（域名）
                        + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.
                        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名
                        + "[a-z]{2,6})" // first level domain- .com or .museum
                        + "(:[0-9]{1,4})?" // 端口- :80
                        + "((/?)|" // a slash isn't required if there is no file name
                        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

                Pattern p = Pattern.compile(regex);
                Matcher matcher = p.matcher(tempurl);
                result = matcher.find();
            }
        }
        return result;
    }

    public static void main(String[] args) {
//        System.out.println(getOrderNumber());

        /*String str = "哈哈sa23哈哈";
        String regex = "^.*[0-9a-zA-z]{4,}.*$";
        System.out.println(matcher(str, regex));*/
        System.out.println(compareVersion("2.1.5", "1.1.5"));
    }
}
